/*
 * Created on Jun 22, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Shell;
import org.scohen.juploadr.Messages;
import org.scohen.juploadr.prefs.Configuration;
import org.scohen.juploadr.ui.ReusableUIFactory;

/**
 * @author steve
 * @author chris
 */
public class AboutDialog {
    private static final int BORDER_WIDTH = 5;
    private static final int BORDER_WIDTH_X2 = BORDER_WIDTH * 2;
    private static final int MAX_OPACITY = 256;
    private static final int WIDTH = 280;
    private static final int INTERVAL = 10;
    private int opacity = MAX_OPACITY;
    private Shell window;
    private ReusableUIFactory uiFactory = ReusableUIFactory.getInstance();
    private Runnable runnable;
    private Image image;
    private ImageData imageData;

    public AboutDialog() {
        init();
    }

    private void init() {
        image = uiFactory.getLogoImage();
        imageData = image.getImageData();
        window = new Shell(uiFactory.getDisplay(), SWT.APPLICATION_MODAL | SWT.NO_BACKGROUND
                | SWT.NO_REDRAW_RESIZE);
        MouseListener disposeOnClick = new MouseAdapter() {
            public void mouseDown(MouseEvent event) {
                AboutDialog.this.mouseDown(event);
            }
        };
        window.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                handlePaint(e);
            }
        });
        window.addMouseListener(disposeOnClick);
        Rectangle clientArea = uiFactory.getDisplay().getClientArea();
        window.setBounds(clientArea.width / 2 - WIDTH / 2, clientArea.height / 2 - WIDTH / 2, WIDTH, WIDTH);
        window.setVisible(true);
        animate();
    }

    private void animate() {
        runnable = new Runnable() {
            public void run() {
                opacity = opacity - 4;
                if (opacity <= 0 || window.isDisposed())
                    return;
                window.redraw();
                uiFactory.getDisplay().timerExec(INTERVAL, runnable);
            }
        };
        uiFactory.getDisplay().timerExec(10, runnable);
    }

    protected void handlePaint(PaintEvent e) {
        paintControl(e);
    }

    protected void mouseDown(MouseEvent event) {
        window.dispose();
    }

    protected void paintControl(PaintEvent event) {
        Image buffer = new Image(event.display, WIDTH, WIDTH);
        GC gc = new GC(buffer);
        if (opacity < MAX_OPACITY - 1) {
            gc.setBackground(uiFactory.getWhiteColor());
            gc.fillRectangle(BORDER_WIDTH, BORDER_WIDTH, WIDTH - BORDER_WIDTH_X2, imageData.height
                    + BORDER_WIDTH_X2);
            gc.drawImage(uiFactory.getLogoImage(), WIDTH / 2 - imageData.width / 2, BORDER_WIDTH_X2);
        } else {
            gc.setBackground(uiFactory.getBlackColor());
            gc.fillRectangle(BORDER_WIDTH, BORDER_WIDTH, WIDTH - BORDER_WIDTH_X2, imageData.height
                    + BORDER_WIDTH_X2);
        }

        gc.setForeground(uiFactory.getThumbnailBorderColor());
        gc.drawRectangle(BORDER_WIDTH, BORDER_WIDTH, WIDTH - BORDER_WIDTH_X2, imageData.height + BORDER_WIDTH_X2);

        ImageData alphaData = new ImageData(WIDTH - BORDER_WIDTH_X2, imageData.height + BORDER_WIDTH_X2,
                imageData.depth, new PaletteData(0xFF, 0xFF00, 0xFF0000));
        alphaData.alpha = opacity;
        Image alphaImage = new Image(uiFactory.getDisplay(), alphaData);
        GC imageGC = new GC(alphaImage);
        imageGC.setBackground(uiFactory.getBlackColor());
        imageGC.fillRectangle(0, 0, WIDTH - BORDER_WIDTH_X2, imageData.height + BORDER_WIDTH_X2);
        gc.drawImage(alphaImage, BORDER_WIDTH, BORDER_WIDTH);
        imageGC.dispose();
        alphaImage.dispose();
        drawText(gc);
        event.gc.drawImage(buffer, 0, 0);
        buffer.dispose();
    }

    private void drawText(GC gc) {
        int lineNumber = 0;
        gc.setBackground(uiFactory.getThumbnailBackgroundColor());
        gc.setForeground(uiFactory.getAboutDialogFontColor());
        drawCenteredString(
                Messages.getString("juploadr.ui.dialog.about.line1") + Configuration.getInstance().getVersion(), lineNumber++, gc); //$NON-NLS-1$
        drawCenteredString(Messages.getString("juploadr.ui.dialog.about.line2"), lineNumber++, gc); //$NON-NLS-1$
        drawCenteredString(Messages.getString("juploadr.ui.dialog.about.line3"), lineNumber++, gc); //$NON-NLS-1$

    }

    private void drawCenteredString(String message, int offset, GC gc) {
        Point stringDimensions = gc.stringExtent(message);
        int stringWidth = stringDimensions.x;
        gc.drawString(message, WIDTH / 2 - stringWidth / 2, image.getBounds().width + 25
                + (offset * stringDimensions.y));
    }

}